/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;

import beans.StoreProviderBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class StoreProviderDAO extends AbstractDAO<StoreProviderBean>{

    @Override
    public void insert(StoreProviderBean storeProvider) throws DAOException {
        try {
             dataBaseManager.openConnection();
             String sqlStatement = "INSERT INTO STORE_PROVIDER(providerId, storeId )values('" + storeProvider.getProviderId() + "','" + storeProvider.getStoreId() + "');";
             dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void delete(String condition) throws DAOException {
        try {
             dataBaseManager.openConnection();
             String sqlStatement = "DELETE FROM store_provider " + condition;
             dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                  dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                  throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    public void update(StoreProviderBean storeProvider, String condition) throws DAOException {
             try {
                dataBaseManager.openConnection();
            String sqlStatement = "UPDATE store_provider SET providerId='" + storeProvider.getProviderId() + "',storeId='" + storeProvider.getStoreId() + "' WHERE " + condition;
           
                dataBaseManager.executeUpdate(sqlStatement);
            }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override
    protected ArrayList<StoreProviderBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE_PROVIDER " + condition;
            ArrayList<StoreProviderBean> storeProviderBeanList = null;
            storeProviderBeanList = createStoreProductBeanList(dataBaseManager.executeQuery(sqlStatement));
            return storeProviderBeanList;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                  dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                  throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }


    @Override
   protected StoreProviderBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM STORE_PROVIDER " + condition;
            StoreProviderBean storeProviderBean = null;
            storeProviderBean = createStoreEmployeBean(dataBaseManager.executeQuery(sqlStatement));
            return storeProviderBean;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                  dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                  throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }


    private ArrayList<StoreProviderBean> createStoreProductBeanList(ResultSet queryResult) throws DAOException{
        try{
            ArrayList<StoreProviderBean> storeProviderBeanList = new ArrayList();
            while(queryResult.next()){
                storeProviderBeanList.add((StoreProviderBean) beansGenerator.generateBean(queryResult, StoreProviderBean.class));
            }
            return storeProviderBeanList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private StoreProviderBean createStoreEmployeBean(ResultSet queryResult) throws DAOException{
        try{
            moveQueryResultToBegin(queryResult);
            return (StoreProviderBean) beansGenerator.generateBean(queryResult, StoreProviderBean.class);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }



     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }

}
